package com.nacrtstudio.webgiftshop.pages;

import java.util.Date;
import java.util.Set;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Order;
import com.nacrtstudio.webgiftshop.model.shop.PackedGift;
import com.nacrtstudio.webgiftshop.model.shop.beans.OrderBean;
import com.nacrtstudio.webgiftshop.subsystems.Mailing;
import com.nacrtstudio.webgiftshop.util.shop.OrderStatus;

public class OrderPage {
	@Inject
	private Request request;
	public void onActivate() {
		try {
			if (controller.getRequest()==null) {
	    		controller.setRequest(request);
			}
		} catch (Exception e) {
		}
	}
	
	@Inject
	private Controller controller;
//	@Persist
//	private Order order;
	@Property
	private PackedGift packGift;

//	public Order getOrder() {
//		return order;
//	}
//
//	public void setOrder(Order order) {
//		this.order = order;
//	}
//	
	@Persist
	private Set<PackedGift> packedGifts;
	
	public Set<PackedGift> getPackedGifts() {
		return packedGifts;
	}

	public void setPackedGifts(Set<PackedGift> packedGifts) {
		this.packedGifts = packedGifts;
	}

	public Double getTotalPrice(){
		Double total = new Double(0);
		for (PackedGift packedGift : getPackedGifts()) {
			total = total+packedGift.getGift().getPrice()+packedGift.getShippingPrice();
		}
		return total;
	}
	
	@InjectPage
	private StatusPage statusPage;
	@InjectPage
	private PayingMethodDetails methodDetails;
	
	
	@InjectPage
	private GiftInfo giftInfo;
	Object onActionFromShow(Long id){
		giftInfo.setGift(controller.findGift(id));
    	return giftInfo;
    }
	
//	Object onActionFromTitleShow(Long id){
//		giftInfo.setGift(controller.findGift(id));
//    	return giftInfo;
//    }
	
	
	
	@Component(id = "packedGiftsCreate")
	private Form packedGiftsForm;
//	@InjectComponent
//    private TextField town;
//	@InjectComponent
//    private TextField street;
//	@InjectComponent
//    private TextField number;
//	@InjectComponent
//    private TextField text;

	@Inject
	private Mailing mailing;
	
	public Object onValidateFromPackedGiftsCreate() {
		try {
			Order order = new OrderBean();
			order.setUser(controller.getLoggedUser());
			order.setPackedGifts(packedGifts);
			order.setOrderId(new Date().getTime());
			for (PackedGift packedGift : getPackedGifts()) {
				packedGift.setOrder(order);
				controller.createPackedGift(packedGift);
				order.getPackedGifts().add(packedGift);
			}
			order.setOrderStatus(OrderStatus.pending);
			order.setTotalPrice(getTotalPrice());
			controller.createOrder(order);
			controller.getLoggedUser().getOrders().add(order);
			controller.updateUser(controller.getLoggedUser());
			controller.deleteShoopingCardFromLoggedUser();
			mailing.sendMail("nacrtstudio@gmail.com", controller.getLoggedUser().geteMail(),"sLjubavlju order", "You made order on site sljubavlju");
			methodDetails.setMessage("Uspesno!!!");
			methodDetails.setOrder(order);
			return methodDetails;
		} catch (Exception e) {
			statusPage.setMessage(e+"");
			return statusPage;
		}
	}
}
